<template>
  <div class="parent">
    <echartsDom v-if="isShowEcharts" style="width: 100%; height: 100%" :width="width" :height="height" :option="option" :type="type" />
    <div v-else>
      <px-empty :image-size="50" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs, ref, watch } from "vue";
import echartsDom from "./echartsDom.vue";
import { queryConsumeChartForDay } from "@/api/waterKanban";
import * as echarts from "echarts";
const props = defineProps({
  width: {
    type: String,
    default: ""
  },
  height: {
    type: String,
    default: ""
  },
  color: {
    type: String,
    default: "#3B82F6"
  },
  type: {
    type: String,
    default: ""
  },
  chartvalue: {
    type: Object,
    required: true
  }
});
const isShowEcharts = ref(false);
const state = reactive({
  option: {},
  yAxis: []
});
const { option, yAxis } = toRefs(state);
const getoptions = () => {
  console.log(props.chartvalue);
  if (props.chartvalue != null && props.chartvalue.yaxis) {
    isShowEcharts.value = true;
    yAxis.value = [
      {
        name: "tCO₂e",
        type: "value"
      }
    ];
    const series = props.chartvalue.yaxis.map(item => {
      if (item.name === "同比变化率") {
        return;
      } else {
        return {
          data: item.value,
          type: "line",
          name: item.name,
          // barWidth: "25px",
          label: {
            show: false,
            color: props.type == "dark" ? "white" : "black"
          },
          emphasis: {
            focus: "series"
          }
        };
      }
    });
    option.value = {
      backgroundColor: props.type == "dark" ? "#001e3b" : "",
      grid: {
        left: "40px",
        top: "55px",
        right: "0px",
        bottom: "10px",
        containLabel: true
      },
      legend: {
        x: "center",
        type: "scroll",
        width: "80%"
      },
      tooltip: {
        confine: true
      },
      xAxis: {
        type: "category",
        boundarGap: false,
        data: props.chartvalue.xaxis
        // axisLabel: {
        //   interval: 0,
        //   rotate: 45
        // }
      },
      yAxis: yAxis.value,
      series: series
    };
  } else {
    isShowEcharts.value = false;
  }
};
watch(
  () => props.chartvalue,
  newOption => {
    console.log(newOption);
    getoptions();
  },
  { immediate: true }
);
</script>

<style scoped lang="scss">
.parent {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>
